/*
 * CC3OpenGLES11StateTracker.m
 *
 * cocos3d 0.6.1
 * Author: Bill Hollings
 * Copyright (c) 2010-2011 The Brenwill Workshop Ltd. All rights reserved.
 * http://www.brenwill.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * http://en.wikipedia.org/wiki/MIT_License
 * 
 * See header file CC3OpenGLES11StateTracker.h for full API documentation.
 */

#include "CC3OpenGLES11StateTracker.h"
#include "ccMacros.h"


#pragma mark -
#pragma mark CC3OpenGLES11StateTracker

//@implementation CC3OpenGLES11StateTracker
//
//+(id) tracker {
//	return [[[self alloc] init] autorelease];
//}
//
//-(void) open {}
//
//-(void) close {}
//
//-(void) openTrackers: (NSArray*) trackers {
//	for (CC3OpenGLES11StateTracker* t in trackers) {
//		[t open];
//	}
//}
//
//-(void) closeTrackers: (NSArray*) trackers {
//	for (CC3OpenGLES11StateTracker* t in trackers) {
//		[t close];
//	}
//}
//
//-(NSString*) description {
//	return [NSString stringWithFormat: @"%@", [self class]];
//}
//
//@end

CC3OpenGLES11StateTracker::CC3OpenGLES11StateTracker()
{
}

CC3OpenGLES11StateTracker::~CC3OpenGLES11StateTracker()
{
}

CC3OpenGLES11StateTracker* CC3OpenGLES11StateTracker::tracker()
{
	CC3OpenGLES11StateTracker* stateTracker = new CC3OpenGLES11StateTracker;
	if (stateTracker) 
	{
		stateTracker->autorelease();
	}
	
	return stateTracker;
}

void CC3OpenGLES11StateTracker::open()
{
}

void CC3OpenGLES11StateTracker::close()
{
}

void CC3OpenGLES11StateTracker::openTrackers(cocos2d::CCArray* trackers)
{
	if (!trackers) 
	{
		return;
	}
	
	int count = trackers->count();
	for (int i = 0; i < count; ++i) 
	{
		CC3OpenGLES11StateTracker* tracker = (CC3OpenGLES11StateTracker*)trackers->objectAtIndex(i);
		if (tracker) 
		{
			tracker->open();
		}
	}
}

void CC3OpenGLES11StateTracker::closeTrackers(cocos2d::CCArray* trackers)
{
	if (!trackers) 
	{
		return;
	}
	
	int count = trackers->count();
	for (int i = 0; i < count; ++i) 
	{
		CC3OpenGLES11StateTracker* tracker = (CC3OpenGLES11StateTracker*)trackers->objectAtIndex(i);
		if (tracker) 
		{
			tracker->close();
		}
	}
}

#pragma mark -
#pragma mark CC3OpenGLES11StateTrackerPrimitive

//@implementation CC3OpenGLES11StateTrackerPrimitive
//
//@synthesize name, originalValueHandling, valueIsKnown;
//
//-(void) setOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	originalValueHandling = origValueHandling;
//	self.valueIsKnown = NO;
//} 
//
//+(CC3GLESStateOriginalValueHandling) defaultOriginalValueHandling {
//	return kCC3GLESStateOriginalValueIgnore;
//}
//
//-(id) init {
//	return [self initForState: 0];
//}
//
//-(id) initForState: (GLenum) aName {
//	return [self initForState: aName andOriginalValueHandling: [[self class] defaultOriginalValueHandling]];
//}
//
//+(id) trackerForState: (GLenum) aName {
//	return [[[self alloc] initForState: aName] autorelease];
//}
//
//-(id) initForState: (GLenum) aName andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	if ( (self = [super init]) ) {
//		self.name = aName;
//		self.originalValueHandling = origValueHandling;
//	}
//	return self;
//}
//
//+(id) trackerForState: (GLenum) aName andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	return [[[self alloc] initForState: aName andOriginalValueHandling: origValueHandling] autorelease];
//}
//
//-(void) open {
//	switch (originalValueHandling) {
//		case kCC3GLESStateOriginalValueIgnore:
//			valueIsKnown = NO;
//			break;
//		case kCC3GLESStateOriginalValueReadOnce:
//		case kCC3GLESStateOriginalValueReadOnceAndRestore:
//			if (valueIsKnown) break;
//		case kCC3GLESStateOriginalValueReadAlways:
//		case kCC3GLESStateOriginalValueReadAlwaysAndRestore:
//			if (name) {
//				[self getGLValue];
//				[self logGetGLValue];
//				[self restoreOriginalValue];
//			} else {
//				valueIsKnown = NO;
//			}
//			break;
//		default:
//			NSAssert3(NO, @"%@ bad original value handling definition %u for capability %@",
//					  self, originalValueHandling, NSStringFromGLEnum(name));
//			break;
//	}
//}
//
//-(void) close {
//	if (originalValueHandling == kCC3GLESStateOriginalValueReadOnceAndRestore ||
//		originalValueHandling == kCC3GLESStateOriginalValueReadAlwaysAndRestore) {
//		[self restoreOriginalValue];
//	}
//}
//
//-(void) restoreOriginalValue {}
//
//-(void) getGLValue {}
//
//-(void) logGetGLValue {}
//
//-(void) setGLValue {}
//
//-(void) logSetValue: (BOOL) wasSet {}
//
//-(NSString*) description {
//	return [NSString stringWithFormat: @"%@ %@", [self class], NSStringFromGLEnum(self.name)];
//}
//
//@end

CC3OpenGLES11StateTrackerPrimitive::CC3OpenGLES11StateTrackerPrimitive() :
	name(0),
	originalValueHandling(kCC3GLESStateOriginalValueIgnore),
	valueIsKnown(false)
{
}

CC3OpenGLES11StateTrackerPrimitive::~CC3OpenGLES11StateTrackerPrimitive()
{
}

void CC3OpenGLES11StateTrackerPrimitive::setName(GLenum name_)
{
	name = name_;
}

GLenum CC3OpenGLES11StateTrackerPrimitive::getName()
{
	return name;
}

void CC3OpenGLES11StateTrackerPrimitive::setOriginalValueHandling(CC3GLESStateOriginalValueHandling originalValueHandling_)
{
	originalValueHandling = originalValueHandling_;
	valueIsKnown = false;
}

CC3GLESStateOriginalValueHandling CC3OpenGLES11StateTrackerPrimitive::getOriginalValueHandling()
{
	return originalValueHandling;
}

void CC3OpenGLES11StateTrackerPrimitive::setValueIsKnown(bool valueIsKnown_)
{
	valueIsKnown = valueIsKnown_;
}

bool CC3OpenGLES11StateTrackerPrimitive::getValueIsKnown()
{
	return valueIsKnown;
}

CC3GLESStateOriginalValueHandling CC3OpenGLES11StateTrackerPrimitive::defaultOriginalValueHandling()
{
	return kCC3GLESStateOriginalValueIgnore;
}

CC3OpenGLES11StateTrackerPrimitive* CC3OpenGLES11StateTrackerPrimitive::initForState(GLenum qName)
{
	return initForState(qName, defaultOriginalValueHandling());
}

CC3OpenGLES11StateTrackerPrimitive* CC3OpenGLES11StateTrackerPrimitive::trackerForState(GLenum qName)
{
	CC3OpenGLES11StateTrackerPrimitive* stateTrackerPrimitive = new CC3OpenGLES11StateTrackerPrimitive;
	if (stateTrackerPrimitive) 
	{
		stateTrackerPrimitive->initForState(qName);
		stateTrackerPrimitive->autorelease();
	}
	
	return stateTrackerPrimitive;
}

CC3OpenGLES11StateTrackerPrimitive* CC3OpenGLES11StateTrackerPrimitive::initForState(GLenum aName, CC3GLESStateOriginalValueHandling origValueHandling)
{
	name = aName;
	originalValueHandling = origValueHandling;
	
	return this;
}

CC3OpenGLES11StateTrackerPrimitive* CC3OpenGLES11StateTrackerPrimitive::trackerForState(GLenum aName, CC3GLESStateOriginalValueHandling origValueHandling)
{
	CC3OpenGLES11StateTrackerPrimitive* stateTrackerPrimitive = new CC3OpenGLES11StateTrackerPrimitive;
	if (stateTrackerPrimitive) 
	{
		stateTrackerPrimitive->initForState(aName, origValueHandling);
		stateTrackerPrimitive->autorelease();
	}
	
	return stateTrackerPrimitive;
}

void CC3OpenGLES11StateTrackerPrimitive::open()
{
	switch (originalValueHandling) 
	{
		case kCC3GLESStateOriginalValueIgnore:
			valueIsKnown = false;
			break;
		case kCC3GLESStateOriginalValueReadOnce:
		case kCC3GLESStateOriginalValueReadOnceAndRestore:
			if (valueIsKnown) break;
		case kCC3GLESStateOriginalValueReadAlways:
		case kCC3GLESStateOriginalValueReadAlwaysAndRestore:
			if (name) 
			{
				getGLValue();
				logGetGLValue();
				restoreOriginalValue();
			} 
			else 
			{
				valueIsKnown = false;
			}
			break;
		default:
			static char szText[TEXT_MAX_LEN];
			sprintf(szText, "%d bad original value handling definition %d for capability %s", 
					(int)this, originalValueHandling, StringFromGLEnum(name));
			CCAssert(false, szText);
			break;
	}
}

void CC3OpenGLES11StateTrackerPrimitive::close()
{
	if (originalValueHandling == kCC3GLESStateOriginalValueReadOnceAndRestore ||
		originalValueHandling == kCC3GLESStateOriginalValueReadAlwaysAndRestore) 
	{
		restoreOriginalValue();
	}
}

void CC3OpenGLES11StateTrackerPrimitive::restoreOriginalValue()
{
}

void CC3OpenGLES11StateTrackerPrimitive::getGLValue()
{
}

void CC3OpenGLES11StateTrackerPrimitive::logGetGLValue()
{
}

void CC3OpenGLES11StateTrackerPrimitive::setGLValue()
{
}

void CC3OpenGLES11StateTrackerPrimitive::logSetValue(bool wasSet)
{
}

#pragma mark -
#pragma mark CC3OpenGLES11StateTrackerBoolean

//@implementation CC3OpenGLES11StateTrackerBoolean
//
//@synthesize value, originalValue, setGLFunction;
//
//-(id) initForState: (GLenum) aName andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	return [self initForState: aName
//			 andGLSetFunction: NULL
//	 andOriginalValueHandling: origValueHandling];
//}
//
//-(id) initForState: (GLenum) aName andGLSetFunction: (CC3SetGLBooleanFunction*) setGLFunc {
//	return [self initForState: aName
//			 andGLSetFunction: setGLFunc
//	 andOriginalValueHandling: [[self class] defaultOriginalValueHandling]];
//}
//
//+(id) trackerForState: (GLenum) aName andGLSetFunction: (CC3SetGLBooleanFunction*) setGLFunc {
//	return [[[self alloc] initForState: aName andGLSetFunction: setGLFunc] autorelease];
//}
//
//-(id) initForState: (GLenum) aName
//  andGLSetFunction: (CC3SetGLBooleanFunction*) setGLFunc
//  andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	if ( (self = [super initForState: aName andOriginalValueHandling: origValueHandling]) ) {
//		setGLFunction = setGLFunc;
//	}
//	return self;
//}
//
//+(id) trackerForState: (GLenum) aName
//	 andGLSetFunction: (CC3SetGLBooleanFunction*) setGLFunc
//	 andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	 return [[[self alloc] initForState: aName
//					   andGLSetFunction: setGLFunc
//			   andOriginalValueHandling: origValueHandling] autorelease];
//}
//
//-(void) setValue: (BOOL) aValue {
//	BOOL wasSet = [self attemptSetValue: aValue];
//	[self logSetValue: wasSet];
//}
//
//-(BOOL) attemptSetValue: (BOOL) aValue {
//	if (!valueIsKnown || aValue != value) {
//		value = aValue;
//		valueIsKnown = YES;
//		[self setGLValue];
//		return YES;
//	} else {
//		return NO;
//	}	
//}
//
//-(void) setGLValue {
//	if( setGLFunction ) {
//		setGLFunction(value ? 1 : 0);
//	}
//}
//
//-(void) logSetValue: (BOOL) wasSet {
//	LogTrace("%@ %@ %@ = %@", [self class], (wasSet ? @"set" : @"reused"),
//			 NSStringFromGLEnum(name), (value ? @"YES" : @"NO"));
//}
//
//-(void) getGLValue {
//	GLboolean glValue;
//	glGetBooleanv(name, &glValue);
//	originalValue = (glValue != 0);
//}
//
//-(void) logGetGLValue {
//	LogTrace("%@ %@ read GL value %@ (was tracking %@)", 
//			 [self class], NSStringFromGLEnum(name), (originalValue ? @"YES" : @"NO"),
//			 (valueIsKnown ? (value ? @"YES" : @"NO") : @"UNKNOWN"));
//}
//
//-(void) restoreOriginalValue {
//	self.value = originalValue;
//}
//
//-(NSString*) description {
//	return [NSString stringWithFormat: @"%@ = %@ (orig %@)", [super description],
//				NSStringFromBoolean(self.value), NSStringFromBoolean(self.originalValue)];
//}
//
//@end

CC3OpenGLES11StateTrackerBoolean::CC3OpenGLES11StateTrackerBoolean() :
	value(0),
	originalValue(0),
	setGLFunction(NULL)
{
}

CC3OpenGLES11StateTrackerBoolean::~CC3OpenGLES11StateTrackerBoolean()
{
}

CC3OpenGLES11StateTrackerBoolean* CC3OpenGLES11StateTrackerBoolean::trackerForState(GLenum aName, CC3SetGLBooleanFunction* setGLFunc)
{
	CC3OpenGLES11StateTrackerBoolean* pState = new CC3OpenGLES11StateTrackerBoolean;
	if (pState) 
	{
		pState->autorelease();
		pState->initForState(aName, setGLFunc);
		return pState;
	}
	
	return NULL;
}

CC3OpenGLES11StateTrackerBoolean* CC3OpenGLES11StateTrackerBoolean::trackerForState(GLenum aName, CC3SetGLBooleanFunction* setGLFunc,
														 CC3GLESStateOriginalValueHandling origValueHandling)
{
	CC3OpenGLES11StateTrackerBoolean* pState = new CC3OpenGLES11StateTrackerBoolean;
	if (pState) 
	{
		pState->autorelease();
		pState->initForState(aName, setGLFunc, origValueHandling);
		return pState;
	}
	
	return NULL;
}

CC3OpenGLES11StateTrackerBoolean* CC3OpenGLES11StateTrackerBoolean::initForState(GLenum aName, CC3GLESStateOriginalValueHandling origValueHandling)
{
	return initForState(aName, NULL, origValueHandling);
}

CC3OpenGLES11StateTrackerBoolean* CC3OpenGLES11StateTrackerBoolean::initForState(GLenum aName, CC3SetGLBooleanFunction* setGLFunc)
{
	return initForState(aName, setGLFunc, defaultOriginalValueHandling());
}

CC3OpenGLES11StateTrackerBoolean* CC3OpenGLES11StateTrackerBoolean::initForState(GLenum aName, CC3SetGLBooleanFunction* setGLFunc,
											   CC3GLESStateOriginalValueHandling origValueHandling)
{
	CC3OpenGLES11StateTrackerPrimitive::initForState(aName, origValueHandling);
	setSetGLFunction(setGLFunc);
	
	return this;
}

void CC3OpenGLES11StateTrackerBoolean::setValue(bool aValue)
{
	bool wasSet = attemptSetValue(aValue);
	logSetValue(wasSet);
}

bool CC3OpenGLES11StateTrackerBoolean::getValue()
{
	return value;
}

void CC3OpenGLES11StateTrackerBoolean::setOriginalValue(bool aValue)
{
	originalValue = aValue;
}

bool CC3OpenGLES11StateTrackerBoolean::getOriginalValue()
{
	return originalValue;
}

void CC3OpenGLES11StateTrackerBoolean::setSetGLFunction(CC3SetGLBooleanFunction* aFunction)
{
	setGLFunction = aFunction;
}

CC3SetGLBooleanFunction* CC3OpenGLES11StateTrackerBoolean::getSetGLFunction()
{
	return setGLFunction;
}

bool CC3OpenGLES11StateTrackerBoolean::attemptSetValue(bool aValue)
{
	if (!valueIsKnown || aValue != value) 
	{
		value = aValue;
		valueIsKnown = true;
		setGLValue();
		return true;
	} 
	else 
	{
		return false;
	}	
}

void CC3OpenGLES11StateTrackerBoolean::setGLValue()
{
	if( setGLFunction ) 
	{
		setGLFunction(value ? 1 : 0);
	}
}

void CC3OpenGLES11StateTrackerBoolean::getGLValue()
{
	GLboolean glValue;
	glGetBooleanv(name, &glValue);
	originalValue = (glValue != 0);
}

void CC3OpenGLES11StateTrackerBoolean::logSetValue(bool wasSet)
{
}

void CC3OpenGLES11StateTrackerBoolean::logGetGLValue()
{
}

void CC3OpenGLES11StateTrackerBoolean::restoreOriginalValue()
{
	setValue(originalValue);
}

#pragma mark -
#pragma mark CC3OpenGLES11StateTrackerCapability

//@implementation CC3OpenGLES11StateTrackerCapability
//
//+(CC3GLESStateOriginalValueHandling) defaultOriginalValueHandling {
//	return kCC3GLESStateOriginalValueReadOnceAndRestore;
//}
//
//-(void) enable {
//	self.value = YES;
//}
//
//-(void) disable {
//	self.value = NO;
//}
//
//-(void) logSetValue: (BOOL) wasSet {
//	LogTrace("%@ %@ %@ = %@", [self class], (wasSet ? @"set" : @"reused"),
//			 NSStringFromGLEnum(name), (value ? @"ENABLED" : @"DISABLED"));
//}
//
//-(void) logGetGLValue {
//	LogTrace("%@ %@ read GL value %@ (was tracking %@)", 
//			 [self class], NSStringFromGLEnum(name), (originalValue ? @"ENABLED" : @"DISABLED"),
//			 (valueIsKnown ? (value ? @"ENABLED" : @"DISABLED") : @"UNKNOWN"));
//}
//
//@end

CC3OpenGLES11StateTrackerCapability::CC3OpenGLES11StateTrackerCapability()
{
}

CC3OpenGLES11StateTrackerCapability::~CC3OpenGLES11StateTrackerCapability()
{
}

CC3GLESStateOriginalValueHandling CC3OpenGLES11StateTrackerCapability::defaultOriginalValueHandling()
{
	return kCC3GLESStateOriginalValueReadOnceAndRestore;
}

void CC3OpenGLES11StateTrackerCapability::enable()
{
	setValue(true);
}

void CC3OpenGLES11StateTrackerCapability::disable()
{
	setValue(false);
}

void CC3OpenGLES11StateTrackerCapability::logSetValue(bool wasSet)
{
}

void CC3OpenGLES11StateTrackerCapability::logGetGLValue()
{
}


#pragma mark -
#pragma mark CC3OpenGLES11StateTrackerFloat

//@implementation CC3OpenGLES11StateTrackerFloat
//
//@synthesize value, originalValue, setGLFunction;
//
//-(id) initForState: (GLenum) aName andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	return [self initForState: aName
//			 andGLSetFunction: NULL
//	 andOriginalValueHandling: origValueHandling];
//}
//
//-(id) initForState: (GLenum) aName andGLSetFunction: (CC3SetGLFloatFunction*) setGLFunc {
//	return [self initForState: aName
//			 andGLSetFunction: setGLFunc
//	 andOriginalValueHandling: [[self class] defaultOriginalValueHandling]];
//}
//
//+(id) trackerForState: (GLenum) aName andGLSetFunction: (CC3SetGLFloatFunction*) setGLFunc {
//	// cast setGLFunc to void to remove bogus compiler warning
//	return [[[self alloc] initForState: aName andGLSetFunction: (void*)setGLFunc] autorelease];
//}
//
//-(id) initForState: (GLenum) aName
//  andGLSetFunction: (CC3SetGLFloatFunction*) setGLFunc
//  andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	if ( (self = [super initForState: aName andOriginalValueHandling: origValueHandling]) ) {
//		setGLFunction = setGLFunc;
//	}
//	return self;
//}
//
//+(id) trackerForState: (GLenum) aName
//	 andGLSetFunction: (CC3SetGLFloatFunction*) setGLFunc
//	 andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	// cast setGLFunc to void to remove bogus compiler warning
//	return [[[self alloc] initForState: aName
//					  andGLSetFunction: (void*)setGLFunc
//			  andOriginalValueHandling: origValueHandling] autorelease];
//}
//
//-(void) setValue: (GLfloat) aValue {
//	BOOL wasSet = [self attemptSetValue: aValue];
//	[self logSetValue: wasSet];
//}
//
//-(BOOL) attemptSetValue: (GLfloat) aValue {
//	if (!valueIsKnown || aValue != value) {
//		value = aValue;
//		valueIsKnown = YES;
//		[self setGLValue];
//		return YES;
//	} else {
//		return NO;
//	}	
//}
//
//-(void) setGLValue {
//	if( setGLFunction ) {
//		setGLFunction(value);
//	}
//}
//
//-(void) logSetValue: (BOOL) wasSet {
//	LogTrace("%@ %@ %@ = %.2f", [self class], (wasSet ? @"set" : @"reused"),
//			 NSStringFromGLEnum(name), value);
//}
//
//-(void) getGLValue {
//	glGetFloatv(name, &originalValue);
//}
//
//-(void) logGetGLValue {
//	LogTrace("%@ %@ read GL value %.2f (was tracking %@)",
//			 [self class], NSStringFromGLEnum(name), originalValue,
//			 (valueIsKnown ? [NSString stringWithFormat: @"%.2f", value] : @"UNKNOWN"));
//}
//
//-(void) restoreOriginalValue {
//	self.value = originalValue;
//}
//
//-(NSString*) description {
//	return [NSString stringWithFormat: @"%@ = %.3f (orig %.3f)",
//			[super description], self.value, self.originalValue];
//}
//
//@end

CC3OpenGLES11StateTrackerFloat::CC3OpenGLES11StateTrackerFloat() :
	value(.0f),
	originalValue(.0f),
	setGLFunction(NULL)
{
}

CC3OpenGLES11StateTrackerFloat::~CC3OpenGLES11StateTrackerFloat()
{
}

CC3OpenGLES11StateTrackerFloat* CC3OpenGLES11StateTrackerFloat::trackerForState(GLenum aName, CC3SetGLFloatFunction* setGLFunc)
{
	CC3OpenGLES11StateTrackerFloat* pState = new CC3OpenGLES11StateTrackerFloat;
	if (pState) 
	{
		pState->autorelease();
		pState->initForState(aName, setGLFunc);
		return pState;
	}
	
	return NULL;
}

CC3OpenGLES11StateTrackerFloat* CC3OpenGLES11StateTrackerFloat::trackerForState(GLenum aName, CC3SetGLFloatFunction* setGLFunc, 
													   CC3GLESStateOriginalValueHandling origValueHandling)
{
	CC3OpenGLES11StateTrackerFloat* pState = new CC3OpenGLES11StateTrackerFloat;
	if (pState) 
	{
		pState->autorelease();
		pState->initForState(aName, setGLFunc, origValueHandling);
		return pState;
	}
	
	return NULL;
}

CC3OpenGLES11StateTrackerFloat* CC3OpenGLES11StateTrackerFloat::initForState(GLenum aName, CC3GLESStateOriginalValueHandling origValueHandling)
{
	return initForState(aName, NULL, origValueHandling);
}

CC3OpenGLES11StateTrackerFloat* CC3OpenGLES11StateTrackerFloat::initForState(GLenum aName, CC3SetGLFloatFunction* setGLFunc)
{
	return initForState(aName, setGLFunc, defaultOriginalValueHandling());
}

CC3OpenGLES11StateTrackerFloat* CC3OpenGLES11StateTrackerFloat::initForState(GLenum aName, CC3SetGLFloatFunction* setGLFunc,
											 CC3GLESStateOriginalValueHandling origValueHandling)
{
	CC3OpenGLES11StateTrackerPrimitive::initForState(aName, origValueHandling);
	setGLFunction = setGLFunc;
	return this;
}

void CC3OpenGLES11StateTrackerFloat::setValue(GLfloat aValue)
{
	bool wasSet = attemptSetValue(aValue);
	logSetValue(wasSet);
}

GLfloat CC3OpenGLES11StateTrackerFloat::getValue()
{
	return value;
}

void CC3OpenGLES11StateTrackerFloat::setOriginalValue(GLfloat aValue)
{
	originalValue = aValue;
}

GLfloat CC3OpenGLES11StateTrackerFloat::getOriginalValue()
{
	return originalValue;
}

void CC3OpenGLES11StateTrackerFloat::setSetGLFunction(CC3SetGLFloatFunction* setGLFunc)
{
	setGLFunction = setGLFunc;
}

CC3SetGLFloatFunction* CC3OpenGLES11StateTrackerFloat::getSetGLFunction()
{
	return setGLFunction;
}

bool CC3OpenGLES11StateTrackerFloat::attemptSetValue(GLfloat aValue)
{
	if (!valueIsKnown || aValue != value) 
	{
		value = aValue;
		valueIsKnown = true;
		setGLValue();
		return true;
	} 
	else 
	{
		return false;
	}
}

void CC3OpenGLES11StateTrackerFloat::setGLValue()
{
	if( setGLFunction ) 
	{
		setGLFunction(value);
	}
}

void CC3OpenGLES11StateTrackerFloat::getGLValue()
{
	glGetFloatv(name, &originalValue);
}

void CC3OpenGLES11StateTrackerFloat::logSetValue(bool wasSet)
{
}

void CC3OpenGLES11StateTrackerFloat::logGetGLValue()
{
}

void CC3OpenGLES11StateTrackerFloat::restoreOriginalValue()
{
	setValue(originalValue);
}

#pragma mark -
#pragma mark CC3OpenGLES11StateTrackerInteger

//@implementation CC3OpenGLES11StateTrackerInteger
//
//@synthesize value, originalValue, setGLFunction;
//
//-(id) initForState: (GLenum) aName andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	return [self initForState: aName
//			 andGLSetFunction: NULL
//	 andOriginalValueHandling: origValueHandling];
//}
//
//-(id) initForState: (GLenum) aName andGLSetFunction: (CC3SetGLIntegerFunction*) setGLFunc {
//	return [self initForState: aName
//			 andGLSetFunction: setGLFunc
//	 andOriginalValueHandling: [[self class] defaultOriginalValueHandling]];
//}
//
//+(id) trackerForState: (GLenum) aName andGLSetFunction: (CC3SetGLIntegerFunction*) setGLFunc {
//	// cast setGLFunc to void to remove bogus compiler warning
//	return [[[self alloc] initForState: aName andGLSetFunction: (void*)setGLFunc] autorelease];
//}
//
//-(id) initForState: (GLenum) aName
//  andGLSetFunction: (CC3SetGLIntegerFunction*) setGLFunc
//  andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	if ( (self = [super initForState: aName andOriginalValueHandling: origValueHandling]) ) {
//		setGLFunction = setGLFunc;
//	}
//	return self;
//}
//
//+(id) trackerForState: (GLenum) aName
//	 andGLSetFunction: (CC3SetGLIntegerFunction*) setGLFunc
//	 andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	// cast setGLFunc to void to remove bogus compiler warning
//	return [[[self alloc] initForState: aName
//					  andGLSetFunction: (void*)setGLFunc
//			  andOriginalValueHandling: origValueHandling] autorelease];
//}
//
//-(void) setValue: (GLint) aValue {
//	BOOL wasSet = [self attemptSetValue: aValue];
//	[self logSetValue: wasSet];
//}
//
//-(BOOL) attemptSetValue: (GLint) aValue {
//	if (!valueIsKnown || aValue != value) {
//		value = aValue;
//		valueIsKnown = YES;
//		[self setGLValue];
//		return YES;
//	} else {
//		return NO;
//	}	
//}
//
//-(void) setGLValue {
//	if( setGLFunction ) {
//		setGLFunction(value);
//	}
//}
//
//-(void) logSetValue: (BOOL) wasSet {
//	LogTrace("%@ %@ %@ = %i", [self class], (wasSet ? @"set" : @"reused"),
//			 NSStringFromGLEnum(name), value);
//}
//
//-(void) getGLValue {
//	glGetIntegerv(name, &originalValue);
//}
//
//-(void) logGetGLValue {
//	LogTrace("%@ %@ read GL value %i (was tracking %@)",
//			 [self class], NSStringFromGLEnum(name), originalValue,
//			 (valueIsKnown ? [NSString stringWithFormat: @"%i", value] : @"UNKNOWN"));
//}
//
//-(void) restoreOriginalValue {
//	self.value = originalValue;
//}
//
//-(NSString*) description {
//	return [NSString stringWithFormat: @"%@ = %i (orig %i)",
//				[super description], self.value, self.originalValue];
//}
//
//@end

CC3OpenGLES11StateTrackerInteger::CC3OpenGLES11StateTrackerInteger() :
	value(0),
	originalValue(0),
	setGLFunction(NULL)
{
}

CC3OpenGLES11StateTrackerInteger::~CC3OpenGLES11StateTrackerInteger()
{
}

CC3OpenGLES11StateTrackerInteger* CC3OpenGLES11StateTrackerInteger::tracker()
{
	CC3OpenGLES11StateTrackerInteger* pState = new CC3OpenGLES11StateTrackerInteger;
	if (pState) 
	{
		pState->autorelease();
	}
	
	return pState;
}

CC3OpenGLES11StateTrackerInteger* CC3OpenGLES11StateTrackerInteger::trackerForState(GLenum aName, CC3SetGLIntegerFunction* setGLFunc)
{
	CC3OpenGLES11StateTrackerInteger* pState = new CC3OpenGLES11StateTrackerInteger;
	if (pState) 
	{
		pState->autorelease();
		pState->initForState(aName, setGLFunc);
	}
	
	return pState;
}

CC3OpenGLES11StateTrackerInteger* CC3OpenGLES11StateTrackerInteger::trackerForState(GLenum aName, CC3SetGLIntegerFunction* setGLFunc,
														 CC3GLESStateOriginalValueHandling origValueHandling)
{
	CC3OpenGLES11StateTrackerInteger* pState = new CC3OpenGLES11StateTrackerInteger;
	if (pState) 
	{
		pState->autorelease();
		pState->initForState(aName, setGLFunc, origValueHandling);
		return pState;
	}
	
	return NULL;
	
}

CC3OpenGLES11StateTrackerInteger* CC3OpenGLES11StateTrackerInteger::trackerForState(GLenum aName, CC3GLESStateOriginalValueHandling origValueHandling)
{
	CC3OpenGLES11StateTrackerInteger* pState = new CC3OpenGLES11StateTrackerInteger;
	if (pState) 
	{
		pState->autorelease();
		pState->initForState(aName, origValueHandling);
		return pState;
	}
	
	return NULL;
}

CC3OpenGLES11StateTrackerInteger* CC3OpenGLES11StateTrackerInteger::initForState(GLenum aName, CC3GLESStateOriginalValueHandling origValueHandling)
{
	return initForState(aName, NULL, origValueHandling);
}

CC3OpenGLES11StateTrackerInteger* CC3OpenGLES11StateTrackerInteger::initForState(GLenum aName, CC3SetGLIntegerFunction* setGLFunc)
{
	return initForState(aName, setGLFunc, defaultOriginalValueHandling());
}

CC3OpenGLES11StateTrackerInteger* CC3OpenGLES11StateTrackerInteger::initForState(GLenum aName, CC3SetGLIntegerFunction* setGLFunc,
											   CC3GLESStateOriginalValueHandling origValueHandling)
{
	CC3OpenGLES11StateTrackerPrimitive::initForState(aName, origValueHandling);
	setGLFunction = setGLFunc;
	return this;
}

void CC3OpenGLES11StateTrackerInteger::setValue(GLint aValue)
{
	bool wasSet = attemptSetValue(aValue);
	logSetValue(wasSet);
}

GLint CC3OpenGLES11StateTrackerInteger::getValue()
{
	return value;
}

void CC3OpenGLES11StateTrackerInteger::setOriginalValue(GLint aOriginalValue)
{
	originalValue = aOriginalValue;
}

GLint CC3OpenGLES11StateTrackerInteger::getOriginalValue()
{
	return originalValue;
}

void CC3OpenGLES11StateTrackerInteger::setSetGLFunction(CC3SetGLIntegerFunction* aFunction)
{
	setGLFunction = aFunction;
}

CC3SetGLIntegerFunction* CC3OpenGLES11StateTrackerInteger::getSetGLFunction()
{
	return setGLFunction;
}

bool CC3OpenGLES11StateTrackerInteger::attemptSetValue(GLint aValue)
{
	if (!valueIsKnown || aValue != value) 
	{
		value = aValue;
		valueIsKnown = true;
		setGLValue();
		return true;
	} 
	else 
	{
		return false;
	}
}

void CC3OpenGLES11StateTrackerInteger::setGLValue()
{
	if( setGLFunction ) 
	{
		setGLFunction(value);
	}
}

void CC3OpenGLES11StateTrackerInteger::getGLValue()
{
	glGetIntegerv(name, &originalValue);
}

void CC3OpenGLES11StateTrackerInteger::logSetValue(bool wasSet)
{
}

void CC3OpenGLES11StateTrackerInteger::logGetGLValue()
{
}

void CC3OpenGLES11StateTrackerInteger::restoreOriginalValue()
{
	setValue(originalValue);
}

#pragma mark -
#pragma mark CC3OpenGLES11StateTrackerEnumeration

//@implementation CC3OpenGLES11StateTrackerEnumeration
//
//@synthesize value, originalValue, setGLFunction;
//
//-(id) initForState: (GLenum) aName andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	return [self initForState: aName
//			 andGLSetFunction: NULL
//	 andOriginalValueHandling: origValueHandling];
//}
//
//-(id) initForState: (GLenum) aName andGLSetFunction: (CC3SetGLEnumerationFunction*) setGLFunc {
//	return [self initForState: aName
//			 andGLSetFunction: setGLFunc
//	 andOriginalValueHandling: [[self class] defaultOriginalValueHandling]];
//}
//
//+(id) trackerForState: (GLenum) aName andGLSetFunction: (CC3SetGLEnumerationFunction*) setGLFunc {
//	// cast setGLFunc to void to remove bogus compiler warning
//	return [[[self alloc] initForState: aName andGLSetFunction: (void*)setGLFunc] autorelease];
//}
//
//-(id) initForState: (GLenum) aName
//  andGLSetFunction: (CC3SetGLEnumerationFunction*) setGLFunc
//andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	if ( (self = [super initForState: aName andOriginalValueHandling: origValueHandling]) ) {
//		setGLFunction = setGLFunc;
//	}
//	return self;
//}
//
//+(id) trackerForState: (GLenum) aName
//	 andGLSetFunction: (CC3SetGLEnumerationFunction*) setGLFunc
//andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	// cast setGLFunc to void to remove bogus compiler warning
//	return [[[self alloc] initForState: aName
//					  andGLSetFunction: (void*)setGLFunc
//			  andOriginalValueHandling: origValueHandling] autorelease];
//}
//
//-(void) setValue: (GLenum) aValue {
//	BOOL wasSet = [self attemptSetValue: aValue];
//	[self logSetValue: wasSet];
//}
//
//-(BOOL) attemptSetValue: (GLenum) aValue {
//	if (!valueIsKnown || aValue != value) {
//		value = aValue;
//		valueIsKnown = YES;
//		[self setGLValue];
//		return YES;
//	} else {
//		return NO;
//	}	
//}
//
//-(void) setGLValue {
//	if( setGLFunction ) {
//		setGLFunction(value);
//	}
//}
//
//-(void) logSetValue: (BOOL) wasSet {
//	LogTrace("%@ %@ %@ = %@", [self class], (wasSet ? @"set" : @"reused"),
//			 NSStringFromGLEnum(name), NSStringFromGLEnum(value));
//}
//
//-(void) getGLValue {
//	glGetIntegerv(name, (GLint*)&originalValue);
//}
//
//-(void) logGetGLValue {
//	LogTrace("%@ %@ read GL value %@ (was tracking %@)",
//			 [self class], NSStringFromGLEnum(name), NSStringFromGLEnum(originalValue),
//			 (valueIsKnown ? NSStringFromGLEnum(value) : @"UNKNOWN"));
//}
//
//-(void) restoreOriginalValue {
//	self.value = originalValue;
//}
//
//-(NSString*) description {
//	return [NSString stringWithFormat: @"%@ = %@ (orig %@)",
//				[super description], NSStringFromGLEnum(self.value), NSStringFromGLEnum(self.originalValue)];
//}
//
//@end

CC3OpenGLES11StateTrackerEnumeration::CC3OpenGLES11StateTrackerEnumeration() :
value(0),
originalValue(0),
setGLFunction(NULL)
{
}

CC3OpenGLES11StateTrackerEnumeration::~CC3OpenGLES11StateTrackerEnumeration()
{
}

CC3OpenGLES11StateTrackerEnumeration* CC3OpenGLES11StateTrackerEnumeration::trackerForState(GLenum aName)
{
	CC3OpenGLES11StateTrackerEnumeration* pState = new CC3OpenGLES11StateTrackerEnumeration;
	if (pState) 
	{
		pState->autorelease();
		pState->initForState(aName, NULL);
		return pState;
	}
	
	return NULL;
}

CC3OpenGLES11StateTrackerEnumeration* CC3OpenGLES11StateTrackerEnumeration::trackerForState(GLenum aName, CC3SetGLEnumerationFunction* setGLFunc)
{
	CC3OpenGLES11StateTrackerEnumeration* pState = new CC3OpenGLES11StateTrackerEnumeration;
	if (pState) 
	{
		pState->autorelease();
		pState->initForState(aName, setGLFunc);
		return pState;
	}
	
	return NULL;
}

CC3OpenGLES11StateTrackerEnumeration* CC3OpenGLES11StateTrackerEnumeration::trackerForState(GLenum aName, CC3SetGLEnumerationFunction* setGLFunc,
																					CC3GLESStateOriginalValueHandling origValueHandling)
{
	CC3OpenGLES11StateTrackerEnumeration* pState = new CC3OpenGLES11StateTrackerEnumeration;
	if (pState) 
	{
		pState->autorelease();
		pState->initForState(aName, setGLFunc, origValueHandling);
		return pState;
	}
	
	return NULL;
	
}

CC3OpenGLES11StateTrackerEnumeration* CC3OpenGLES11StateTrackerEnumeration::initForState(GLenum aName, CC3GLESStateOriginalValueHandling origValueHandling)
{
	return initForState(aName, NULL, origValueHandling);
}

CC3OpenGLES11StateTrackerEnumeration* CC3OpenGLES11StateTrackerEnumeration::initForState(GLenum aName, CC3SetGLEnumerationFunction* setGLFunc)
{
	return initForState(aName, setGLFunc, defaultOriginalValueHandling());
}

CC3OpenGLES11StateTrackerEnumeration* CC3OpenGLES11StateTrackerEnumeration::initForState(GLenum aName, CC3SetGLEnumerationFunction* setGLFunc,
																				 CC3GLESStateOriginalValueHandling origValueHandling)
{
	CC3OpenGLES11StateTrackerPrimitive::initForState(aName, origValueHandling);
	setGLFunction = setGLFunc;
	return this;
}

void CC3OpenGLES11StateTrackerEnumeration::setValue(GLenum aValue)
{
	bool wasSet = attemptSetValue(aValue);
	logSetValue(wasSet);
}

GLenum CC3OpenGLES11StateTrackerEnumeration::getValue()
{
	return value;
}

void CC3OpenGLES11StateTrackerEnumeration::setOriginalValue(GLenum aOriginalValue)
{
	originalValue = aOriginalValue;
}

GLenum CC3OpenGLES11StateTrackerEnumeration::getOriginalValue()
{
	return originalValue;
}

void CC3OpenGLES11StateTrackerEnumeration::setSetGLFunction(CC3SetGLEnumerationFunction* aFunction)
{
	setGLFunction = aFunction;
}

CC3SetGLEnumerationFunction* CC3OpenGLES11StateTrackerEnumeration::getSetGLFunction()
{
	return setGLFunction;
}

bool CC3OpenGLES11StateTrackerEnumeration::attemptSetValue(GLenum aValue)
{
	if (!valueIsKnown || aValue != value) 
	{
		value = aValue;
		valueIsKnown = true;
		setGLValue();
		return true;
	} 
	else 
	{
		return false;
	}
}

void CC3OpenGLES11StateTrackerEnumeration::setGLValue()
{
	if( setGLFunction ) 
	{
		setGLFunction(value);
	}
}

void CC3OpenGLES11StateTrackerEnumeration::getGLValue()
{
	glGetIntegerv(name, (GLint*)&originalValue);
}

void CC3OpenGLES11StateTrackerEnumeration::logSetValue(bool wasSet)
{
}

void CC3OpenGLES11StateTrackerEnumeration::logGetGLValue()
{
}

void CC3OpenGLES11StateTrackerEnumeration::restoreOriginalValue()
{
	setValue(originalValue);
}

#pragma mark -
#pragma mark CC3OpenGLES11StateTrackerColor

//@implementation CC3OpenGLES11StateTrackerColor
//
//@synthesize value, originalValue, setGLFunction;
//
//-(id) initForState: (GLenum) aName andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	return [self initForState: aName
//			 andGLSetFunction: NULL
//	 andOriginalValueHandling: origValueHandling];
//}
//
//-(id) initForState: (GLenum) aName andGLSetFunction: (CC3SetGLColorFunction*) setGLFunc {
//	return [self initForState: aName
//			 andGLSetFunction: setGLFunc
//	 andOriginalValueHandling: [[self class] defaultOriginalValueHandling]];
//}
//
//+(id) trackerForState: (GLenum) aName andGLSetFunction: (CC3SetGLColorFunction*) setGLFunc {
//	// cast setGLFunc to void to remove bogus compiler warning
//	return [[[self alloc] initForState: aName andGLSetFunction: (void*)setGLFunc] autorelease];
//}
//
//-(id) initForState: (GLenum) aName
//  andGLSetFunction: (CC3SetGLColorFunction*) setGLFunc
//andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	if ( (self = [super initForState: aName andOriginalValueHandling: origValueHandling]) ) {
//		setGLFunction = setGLFunc;
//	}
//	return self;
//}
//
//+(id) trackerForState: (GLenum) aName
//	 andGLSetFunction: (CC3SetGLColorFunction*) setGLFunc
//andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	// cast setGLFunc to void to remove bogus compiler warning
//	return [[[self alloc] initForState: aName
//					  andGLSetFunction: (void*)setGLFunc
//			  andOriginalValueHandling: origValueHandling] autorelease];
//}
//
//-(void) setValue: (ccColor4F) aColor {
//	BOOL wasSet = [self attemptSetValue: aColor];
//	[self logSetValue: wasSet];
//}
//
//-(BOOL) attemptSetValue: (ccColor4F) aColor {
//	if (!valueIsKnown || !CCC4FAreEqual(aColor, value)) {
//		value = aColor;
//		valueIsKnown = YES;
//		[self setGLValue];
//		return YES;
//	} else {
//		return NO;
//	}	
//}
//
//-(void) setGLValue {
//	if( setGLFunction ) {
//		setGLFunction(value.r, value.g, value.b, value.a);
//	}
//}
//
//-(void) logSetValue: (BOOL) wasSet {
//	LogTrace("%@ %@ %@ = %@", [self class], (wasSet ? @"set" : @"reused"),
//			 NSStringFromGLEnum(name), NSStringFromCCC4F(value));
//}
//
//-(void) getGLValue {
//	glGetFloatv(name, (GLfloat*)&originalValue);
//}
//
//-(void) logGetGLValue {
//	LogTrace("%@ %@ read GL value %@ (was tracking %@)", 
//			 [self class], NSStringFromGLEnum(name), NSStringFromCCC4F(originalValue),
//			 (valueIsKnown ? NSStringFromCCC4F(value) : @"UNKNOWN"));
//}
//
//-(void) restoreOriginalValue {
//	self.value = originalValue;
//}
//
//-(NSString*) description {
//	return [NSString stringWithFormat: @"%@ = %@ (orig %@)",
//				[super description], NSStringFromCCC4F(self.value), NSStringFromCCC4F(self.originalValue)];
//}
//
//@end

CC3OpenGLES11StateTrackerColor::CC3OpenGLES11StateTrackerColor() :
value(cocos2d::ccc4F(.0f, .0f, .0f, .0f)),
originalValue(cocos2d::ccc4F(.0f, .0f, .0f, .0f)),
setGLFunction(NULL)
{
}

CC3OpenGLES11StateTrackerColor::~CC3OpenGLES11StateTrackerColor()
{
}

CC3OpenGLES11StateTrackerColor* CC3OpenGLES11StateTrackerColor::trackerForState(GLenum aName, CC3SetGLColorFunction* setGLFunc)
{
	CC3OpenGLES11StateTrackerColor* pState = new CC3OpenGLES11StateTrackerColor;
	if (pState) 
	{
		pState->autorelease();
		pState->initForState(aName, setGLFunc);
		return pState;
	}
	
	return NULL;
}

CC3OpenGLES11StateTrackerColor* CC3OpenGLES11StateTrackerColor::trackerForState(GLenum aName, CC3SetGLColorFunction* setGLFunc,
																							CC3GLESStateOriginalValueHandling origValueHandling)
{
	CC3OpenGLES11StateTrackerColor* pState = new CC3OpenGLES11StateTrackerColor;
	if (pState) 
	{
		pState->autorelease();
		pState->initForState(aName, setGLFunc, origValueHandling);
		return pState;
	}
	
	return NULL;
	
}

CC3OpenGLES11StateTrackerColor* CC3OpenGLES11StateTrackerColor::initForState(GLenum aName, CC3GLESStateOriginalValueHandling origValueHandling)
{
	return initForState(aName, NULL, origValueHandling);
}

CC3OpenGLES11StateTrackerColor* CC3OpenGLES11StateTrackerColor::initForState(GLenum aName, CC3SetGLColorFunction* setGLFunc)
{
	return initForState(aName, setGLFunc, defaultOriginalValueHandling());
}

CC3OpenGLES11StateTrackerColor* CC3OpenGLES11StateTrackerColor::initForState(GLenum aName, CC3SetGLColorFunction* setGLFunc,
																						 CC3GLESStateOriginalValueHandling origValueHandling)
{
	CC3OpenGLES11StateTrackerPrimitive::initForState(aName, origValueHandling);
	setGLFunction = setGLFunc;
	return this;
}

void CC3OpenGLES11StateTrackerColor::setValue(cocos2d::ccColor4F aValue)
{
	bool wasSet = attemptSetValue(aValue);
	logSetValue(wasSet);
}

cocos2d::ccColor4F CC3OpenGLES11StateTrackerColor::getValue()
{
	return value;
}

void CC3OpenGLES11StateTrackerColor::setOriginalValue(cocos2d::ccColor4F aOriginalValue)
{
	originalValue = aOriginalValue;
}

cocos2d::ccColor4F CC3OpenGLES11StateTrackerColor::getOriginalValue()
{
	return originalValue;
}

void CC3OpenGLES11StateTrackerColor::setSetGLFunction(CC3SetGLColorFunction* aFunction)
{
	setGLFunction = aFunction;
}

CC3SetGLColorFunction* CC3OpenGLES11StateTrackerColor::getSetGLFunction()
{
	return setGLFunction;
}

bool CC3OpenGLES11StateTrackerColor::attemptSetValue(cocos2d::ccColor4F aValue)
{
	if (!valueIsKnown || !CCC4FAreEqual(aValue, value)) 
	{
		value = aValue;
		valueIsKnown = true;
		setGLValue();
		return true;
	} 
	else 
	{
		return false;
	}	
}

void CC3OpenGLES11StateTrackerColor::setGLValue()
{
	if( setGLFunction ) 
	{
		setGLFunction(value.r, value.g, value.b, value.a);
	}
}

void CC3OpenGLES11StateTrackerColor::getGLValue()
{
	glGetFloatv(name, (GLfloat*)&originalValue);
}

void CC3OpenGLES11StateTrackerColor::logSetValue(bool wasSet)
{
}

void CC3OpenGLES11StateTrackerColor::logGetGLValue()
{
}

void CC3OpenGLES11StateTrackerColor::restoreOriginalValue()
{
	setValue(originalValue);
}

#pragma mark -
#pragma mark CC3OpenGLES11StateTrackerColorFixedAndFloat

//@implementation CC3OpenGLES11StateTrackerColorFixedAndFloat
//
//@synthesize fixedValue, setGLFunctionFixed;
//
//-(id) initForState: (GLenum) aName andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	return [self initForState: aName
//			 andGLSetFunction: NULL
//		andGLSetFunctionFixed: NULL
//	 andOriginalValueHandling: origValueHandling];
//}
//
//-(id) initForState: (GLenum) aName
//  andGLSetFunction: (CC3SetGLColorFunction*) setGLFunc
//andGLSetFunctionFixed:  (CC3SetGLColorFunctionFixed*) setGLFuncFixed {
//	return [self initForState: aName
//			 andGLSetFunction: setGLFunc
//		andGLSetFunctionFixed: setGLFuncFixed
//	 andOriginalValueHandling: [[self class] defaultOriginalValueHandling]];
//}
//
//+(id) trackerForState: (GLenum) aName
//	 andGLSetFunction: (CC3SetGLColorFunction*) setGLFunc
//andGLSetFunctionFixed:  (CC3SetGLColorFunctionFixed*) setGLFuncFixed {
//	// cast setGLFunc & setGLFuncFixed to void to remove bogus compiler warning
//	return [[[self alloc] initForState: aName
//					  andGLSetFunction: (void*)setGLFunc
//				 andGLSetFunctionFixed: (void*)setGLFuncFixed] autorelease];
//}
//
//-(id) initForState: (GLenum) aName
//  andGLSetFunction: (CC3SetGLColorFunction*) setGLFunc
//andGLSetFunctionFixed:  (CC3SetGLColorFunctionFixed*) setGLFuncFixed
//andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	if ( (self = [super initForState: aName
//					andGLSetFunction: setGLFunc
//			andOriginalValueHandling: origValueHandling]) ) {
//		setGLFunctionFixed = setGLFuncFixed;
//	}
//	return self;
//}
//
//+(id) trackerForState: (GLenum) aName
//	 andGLSetFunction: (CC3SetGLColorFunction*) setGLFunc
//andGLSetFunctionFixed:  (CC3SetGLColorFunctionFixed*) setGLFuncFixed
//andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	// cast setGLFunc to void to remove bogus compiler warning
//	return [[[self alloc] initForState: aName
//					  andGLSetFunction: (void*)setGLFunc
//				 andGLSetFunctionFixed: setGLFuncFixed
//			  andOriginalValueHandling: origValueHandling] autorelease];
//}
//
//-(void) setFixedValue: (ccColor4B) aColor {
//	BOOL wasSet = [self attemptSetFixedValue: aColor];
//	[self logSetFixedValue: wasSet];
//}
//
//-(BOOL) attemptSetValue: (ccColor4F) aColor {
//	BOOL wasSet = [super attemptSetValue: aColor];
//	if (wasSet) {
//		fixedValueIsKnown = NO;
//	}
//	return wasSet;
//}
//
//-(BOOL) attemptSetFixedValue: (ccColor4B) aColor {
//	if (!fixedValueIsKnown || fixedValue.r != aColor.r || fixedValue.g != aColor.g
//						   || fixedValue.b != aColor.b || fixedValue.a != aColor.a) {
//		fixedValue = aColor;
//		fixedValueIsKnown = YES;
//		valueIsKnown = NO;
//		[self setGLFixedValue];
//		return YES;
//	} else {
//		return NO;
//	}	
//}
//
//-(void) setGLFixedValue {
//	if( setGLFunctionFixed ) {
//		setGLFunctionFixed(fixedValue.r, fixedValue.g, fixedValue.b, fixedValue.a);
//	}
//}
//
//-(void) logSetFixedValue: (BOOL) wasSet {
//	LogTrace("%@ %@ %@ = (%u, %u, %u, %u)", [self class], (wasSet ? @"set" : @"reused"),
//			 NSStringFromGLEnum(name), fixedValue.r, fixedValue.g, fixedValue.b, fixedValue.a);
//}
//
//@end

CC3OpenGLES11StateTrackerColorFixedAndFloat::CC3OpenGLES11StateTrackerColorFixedAndFloat() :
	setGLFunctionFixed(NULL),
	fixedValueIsKnown(false)
{
}

CC3OpenGLES11StateTrackerColorFixedAndFloat::~CC3OpenGLES11StateTrackerColorFixedAndFloat()
{
}

CC3OpenGLES11StateTrackerColorFixedAndFloat* CC3OpenGLES11StateTrackerColorFixedAndFloat::trackerForState(GLenum aName, CC3SetGLColorFunction* setGLFunc,
																	CC3SetGLColorFunctionFixed* setGLFuncFixed)
{
	CC3OpenGLES11StateTrackerColorFixedAndFloat* pState = new CC3OpenGLES11StateTrackerColorFixedAndFloat;
	if (pState) 
	{
		pState->autorelease();
		pState->initForState(aName, setGLFunc, setGLFuncFixed);
		return pState;
	}
	
	return NULL;
}

CC3OpenGLES11StateTrackerColorFixedAndFloat* CC3OpenGLES11StateTrackerColorFixedAndFloat::trackerForState(GLenum aName, CC3SetGLColorFunction* setGLFunc,
																	CC3SetGLColorFunctionFixed* setGLFuncFixed,
																	CC3GLESStateOriginalValueHandling origValueHandling)
{
	CC3OpenGLES11StateTrackerColorFixedAndFloat* pState = new CC3OpenGLES11StateTrackerColorFixedAndFloat;
	if (pState) 
	{
		pState->autorelease();
		pState->initForState(aName, setGLFunc, setGLFuncFixed, origValueHandling);
		return pState;
	}
	
	return NULL;
}

CC3OpenGLES11StateTrackerColorFixedAndFloat* CC3OpenGLES11StateTrackerColorFixedAndFloat::initForState(GLenum aName, CC3SetGLColorFunction* setGLFunc, 
														  CC3SetGLColorFunctionFixed* setGLFuncFixed)
{
	return initForState(aName, setGLFunc, NULL, defaultOriginalValueHandling());
}

CC3OpenGLES11StateTrackerColorFixedAndFloat* CC3OpenGLES11StateTrackerColorFixedAndFloat::initForState(GLenum aName, CC3SetGLColorFunction* setGLFunc,
														  CC3SetGLColorFunctionFixed* setGLFuncFixed,
														  CC3GLESStateOriginalValueHandling origValueHandling)
{
	CC3OpenGLES11StateTrackerColor::initForState(aName, setGLFunc, origValueHandling);
	setGLFunctionFixed = setGLFuncFixed;
	return this;
}

CC3OpenGLES11StateTrackerColorFixedAndFloat* CC3OpenGLES11StateTrackerColorFixedAndFloat::initForState(GLenum aName, CC3GLESStateOriginalValueHandling origValueHandling)
{
	return initForState(aName, NULL, NULL, origValueHandling);
}

void CC3OpenGLES11StateTrackerColorFixedAndFloat::setFixedValue(cocos2d::ccColor4B aColor)
{
	bool wasSet = attemptSetFixedValue(aColor);
	logSetFixedValue(wasSet);
}

cocos2d::ccColor4B CC3OpenGLES11StateTrackerColorFixedAndFloat::getFixedValue()
{
	return fixedValue;
}

void CC3OpenGLES11StateTrackerColorFixedAndFloat::setSetGLFunctionFixed(CC3SetGLColorFunctionFixed* aFunction)
{
	setGLFunctionFixed = aFunction;
}

CC3SetGLColorFunctionFixed* CC3OpenGLES11StateTrackerColorFixedAndFloat::getSetGLFunctionFixed()
{
	return setGLFunctionFixed;
}

bool CC3OpenGLES11StateTrackerColorFixedAndFloat::attemptSetValue(cocos2d::ccColor4F aColor)
{
	bool wasSet = CC3OpenGLES11StateTrackerColor::attemptSetValue(aColor);
	if (wasSet) 
	{
		fixedValueIsKnown = false;
	}
	
	return wasSet;
}

bool CC3OpenGLES11StateTrackerColorFixedAndFloat::attemptSetFixedValue(cocos2d::ccColor4B aColor)
{
	if (!fixedValueIsKnown || fixedValue.r != aColor.r || fixedValue.g != aColor.g
		|| fixedValue.b != aColor.b || fixedValue.a != aColor.a) 
	{
		fixedValue = aColor;
		fixedValueIsKnown = true;
		valueIsKnown = false;
		setGLFixedValue();
		return true;
	} 
	else 
	{
		return false;
	}	
}

void CC3OpenGLES11StateTrackerColorFixedAndFloat::setGLFixedValue()
{
	if( setGLFunctionFixed ) 
	{
		setGLFunctionFixed(fixedValue.r, fixedValue.g, fixedValue.b, fixedValue.a);
	}
}

void CC3OpenGLES11StateTrackerColorFixedAndFloat::logSetFixedValue(bool wasSet)
{
}

#pragma mark -
#pragma mark CC3OpenGLES11StateTrackerViewport

//@implementation CC3OpenGLES11StateTrackerViewport
//
//@synthesize value, originalValue, setGLFunction;
//
//-(id) initForState: (GLenum) aName andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	return [self initForState: aName
//			 andGLSetFunction: NULL
//	 andOriginalValueHandling: origValueHandling];
//}
//
//-(id) initForState: (GLenum) aName andGLSetFunction: (CC3SetGLViewportFunction*) setGLFunc {
//	return [self initForState: aName
//			 andGLSetFunction: setGLFunc
//	 andOriginalValueHandling: [[self class] defaultOriginalValueHandling]];
//}
//
//+(id) trackerForState: (GLenum) aName andGLSetFunction: (CC3SetGLViewportFunction*) setGLFunc {
//	// cast setGLFunc to void to remove bogus compiler warning
//	return [[[self alloc] initForState: aName andGLSetFunction: (void*)setGLFunc] autorelease];
//}
//
//-(id) initForState: (GLenum) aName
//  andGLSetFunction: (CC3SetGLViewportFunction*) setGLFunc
//  andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	if ( (self = [super initForState: aName andOriginalValueHandling: origValueHandling]) ) {
//		setGLFunction = setGLFunc;
//	}
//	return self;
//}
//
//+(id) trackerForState: (GLenum) aName
//	 andGLSetFunction: (CC3SetGLViewportFunction*) setGLFunc
//	 andOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	// cast setGLFunc to void to remove bogus compiler warning
//	return [[[self alloc] initForState: aName
//					  andGLSetFunction: (void*)setGLFunc
//			  andOriginalValueHandling: origValueHandling] autorelease];
//}
//
//-(void) setValue: (CC3Viewport) aViewport {
//	BOOL wasSet = [self attemptSetValue: aViewport];
//	[self logSetValue: wasSet];
//}
//
//-(BOOL) attemptSetValue: (CC3Viewport) aViewport {
//	if (!valueIsKnown || !CC3ViewportsAreEqual(aViewport, value)) {
//		value = aViewport;
//		valueIsKnown = YES;
//		[self setGLValue];
//		return YES;
//	} else {
//		return NO;
//	}	
//}
//
//-(void) setGLValue {
//	if( setGLFunction ) {
//		setGLFunction(value.x, value.y, value.w, value.h);
//	}
//}
//
//-(void) logSetValue: (BOOL) wasSet {
//	LogTrace("%@ %@ %@ = %@", [self class], (wasSet ? @"set" : @"reused"),
//			 NSStringFromGLEnum(name), NSStringFromCC3Viewport(value));
//}
//
//-(void) getGLValue {
//	glGetIntegerv(name, (GLint*)&originalValue);
//}
//
//-(void) logGetGLValue {
//	LogTrace("%@ %@ read GL value %@ (was tracking %@)", 
//			 [self class], NSStringFromGLEnum(name), NSStringFromCC3Viewport(originalValue),
//			 (valueIsKnown ? NSStringFromCC3Viewport(value) : @"UNKNOWN"));
//}
//
//-(void) restoreOriginalValue {
//	self.value = originalValue;
//}
//
//-(NSString*) description {
//	return [NSString stringWithFormat: @"%@ = %@ (orig %@)",
//			[super description], NSStringFromCC3Viewport(self.value), NSStringFromCC3Viewport(self.originalValue)];
//}
//
//@end

CC3OpenGLES11StateTrackerViewport::CC3OpenGLES11StateTrackerViewport() :
value(CC3ViewportMake(0, 0, 0, 0)),
originalValue(CC3ViewportMake(0, 0, 0, 0)),
setGLFunction(NULL)
{
}

CC3OpenGLES11StateTrackerViewport::~CC3OpenGLES11StateTrackerViewport()
{
}

CC3OpenGLES11StateTrackerViewport* CC3OpenGLES11StateTrackerViewport::trackerForState(GLenum aName, CC3SetGLViewportFunction* setGLFunc)
{
	CC3OpenGLES11StateTrackerViewport* pState = new CC3OpenGLES11StateTrackerViewport;
	if (pState) 
	{
		pState->autorelease();
		pState->initForState(aName, setGLFunc);
		return pState;
	}
	
	return NULL;
}

CC3OpenGLES11StateTrackerViewport* CC3OpenGLES11StateTrackerViewport::trackerForState(GLenum aName, CC3SetGLViewportFunction* setGLFunc,
																				CC3GLESStateOriginalValueHandling origValueHandling)
{
	CC3OpenGLES11StateTrackerViewport* pState = new CC3OpenGLES11StateTrackerViewport;
	if (pState) 
	{
		pState->autorelease();
		pState->initForState(aName, setGLFunc, origValueHandling);
		return pState;
	}
	
	return NULL;
	
}

CC3OpenGLES11StateTrackerViewport* CC3OpenGLES11StateTrackerViewport::initForState(GLenum aName, CC3GLESStateOriginalValueHandling origValueHandling)
{
	return initForState(aName, NULL, origValueHandling);
}

CC3OpenGLES11StateTrackerViewport* CC3OpenGLES11StateTrackerViewport::initForState(GLenum aName, CC3SetGLViewportFunction* setGLFunc)
{
	return initForState(aName, setGLFunc, defaultOriginalValueHandling());
}

CC3OpenGLES11StateTrackerViewport* CC3OpenGLES11StateTrackerViewport::initForState(GLenum aName, CC3SetGLViewportFunction* setGLFunc,
																			 CC3GLESStateOriginalValueHandling origValueHandling)
{
	CC3OpenGLES11StateTrackerPrimitive::initForState(aName, origValueHandling);
	setGLFunction = setGLFunc;
	return this;
}

void CC3OpenGLES11StateTrackerViewport::setValue(CC3Viewport aValue)
{
	bool wasSet = attemptSetValue(aValue);
	logSetValue(wasSet);
}

CC3Viewport CC3OpenGLES11StateTrackerViewport::getValue()
{
	return value;
}

void CC3OpenGLES11StateTrackerViewport::setOriginalValue(CC3Viewport aOriginalValue)
{
	originalValue = aOriginalValue;
}

CC3Viewport CC3OpenGLES11StateTrackerViewport::getOriginalValue()
{
	return originalValue;
}

void CC3OpenGLES11StateTrackerViewport::setSetGLFunction(CC3SetGLViewportFunction* aFunction)
{
	setGLFunction = aFunction;
}

CC3SetGLViewportFunction* CC3OpenGLES11StateTrackerViewport::getSetGLFunction()
{
	return setGLFunction;
}

bool CC3OpenGLES11StateTrackerViewport::attemptSetValue(CC3Viewport aValue)
{
	if (!valueIsKnown || !CC3ViewportsAreEqual(aValue, value)) 
	{
		value = aValue;
		valueIsKnown = true;
		setGLValue();
		return true;
	} 
	else 
	{
		return false;
			
	}		
}

void CC3OpenGLES11StateTrackerViewport::setGLValue()
{
	if( setGLFunction ) 
	{
		setGLFunction(value.x, value.y, value.w, value.h);
	}
}

void CC3OpenGLES11StateTrackerViewport::getGLValue()
{
	glGetIntegerv(name, (GLint*)&originalValue);
}

void CC3OpenGLES11StateTrackerViewport::logSetValue(bool wasSet)
{
}

void CC3OpenGLES11StateTrackerViewport::logGetGLValue()
{
}

void CC3OpenGLES11StateTrackerViewport::restoreOriginalValue()
{
	setValue(originalValue);
}


#pragma mark -
#pragma mark CC3OpenGLES11StateTrackerPointer

//@implementation CC3OpenGLES11StateTrackerPointer
//
//@synthesize value, originalValue;
//
//-(void) setValue: (GLvoid*) aValue {
//	BOOL wasSet = [self attemptSetValue: aValue];
//	[self logSetValue: wasSet];
//}
//
//-(BOOL) attemptSetValue: (GLvoid*) aValue {
//	if (!valueIsKnown || aValue != value) {
//		value = aValue;
//		valueIsKnown = YES;
//		[self setGLValue];
//		return YES;
//	} else {
//		return NO;
//	}	
//}
//
//-(void) logSetValue: (BOOL) wasSet {
//	LogTrace("%@ %@ %@ = %p", [self class], (wasSet ? @"set" : @"reused"),
//			 NSStringFromGLEnum(name), value);
//}
//
//-(void) getGLValue {
//	glGetIntegerv(name, (GLint*)&originalValue);
//}
//
//-(void) logGetGLValue {
//	LogTrace("%@ %@ read GL value %p (was tracking %@)",
//			 [self class], NSStringFromGLEnum(name), originalValue,
//			 (valueIsKnown ? [NSString stringWithFormat: @"%p", value] : @"UNKNOWN"));
//}
//
//-(void) restoreOriginalValue {
//	self.value = originalValue;
//}
//
//-(NSString*) description {
//	return [NSString stringWithFormat: @"%@ = %p (orig %p)",
//			[super description], self.value, self.originalValue];
//}
//
//@end

CC3OpenGLES11StateTrackerPointer::CC3OpenGLES11StateTrackerPointer() :
	value(NULL),
	originalValue(NULL)
{
	
}	

CC3OpenGLES11StateTrackerPointer::~CC3OpenGLES11StateTrackerPointer()
{
	
}

CC3OpenGLES11StateTrackerPointer* CC3OpenGLES11StateTrackerPointer::tracker()
{
	CC3OpenGLES11StateTrackerPointer* pState = new CC3OpenGLES11StateTrackerPointer;
	if (pState) 
	{
		pState->autorelease();
	}
	
	return pState;
}

void CC3OpenGLES11StateTrackerPointer::setValue(GLvoid* aValue)
{
	bool wasSet = attemptSetValue(aValue);
	logSetValue(wasSet);
}

GLvoid* CC3OpenGLES11StateTrackerPointer::getValue()
{
	return value;
}

void CC3OpenGLES11StateTrackerPointer::setOriginalValue(GLvoid* aValue)
{
	originalValue = aValue;
}

GLvoid* CC3OpenGLES11StateTrackerPointer::getOriginalValue()
{
	return originalValue;
}

bool CC3OpenGLES11StateTrackerPointer::attemptSetValue(GLvoid* aValue)
{
	if (!valueIsKnown || aValue != value) 
	{
		value = aValue;
		valueIsKnown = true;
		setGLValue();
		return true;
	} 
	else 
	{
		return false;
	}	
}

void CC3OpenGLES11StateTrackerPointer::getGLValue()
{
	glGetIntegerv(name, (GLint*)&originalValue);
}

void CC3OpenGLES11StateTrackerPointer::logSetValue(bool wasSet)
{
}

void CC3OpenGLES11StateTrackerPointer::logGetGLValue()
{
}

void CC3OpenGLES11StateTrackerPointer::restoreOriginalValue()
{
	setValue(originalValue);
}


#pragma mark -
#pragma mark CC3OpenGLES11StateTrackerVector

//@implementation CC3OpenGLES11StateTrackerVector
//
//@synthesize value, originalValue;
//
//-(void) setValue: (CC3Vector) aVector {
//	BOOL wasSet = [self attemptSetValue: aVector];
//	[self logSetValue: wasSet];
//}
//
//-(BOOL) attemptSetValue: (CC3Vector) aVector {
//	if (!valueIsKnown || !CC3VectorsAreEqual(aVector, value)) {
//		value = aVector;
//		valueIsKnown = YES;
//		[self setGLValue];
//		return YES;
//	} else {
//		return NO;
//	}	
//}
//
//-(void) logSetValue: (BOOL) wasSet {
//	LogTrace("%@ %@ %@ = %@", [self class], (wasSet ? @"set" : @"reused"),
//			 NSStringFromGLEnum(name), NSStringFromCC3Vector(value));
//}
//
//-(void) getGLValue {
//	glGetFloatv(name, (GLfloat*)&originalValue);
//}
//
//-(void) logGetGLValue {
//	LogTrace("%@ %@ read GL value %@ (was tracking %@)", 
//			 [self class], NSStringFromGLEnum(name), NSStringFromCC3Vector(originalValue),
//			 (valueIsKnown ? NSStringFromCC3Vector(value) : @"UNKNOWN"));
//}
//
//-(void) restoreOriginalValue {
//	self.value = originalValue;
//}
//
//-(NSString*) description {
//	return [NSString stringWithFormat: @"%@ = %@ (orig %@)",
//			[super description], NSStringFromCC3Vector(self.value), NSStringFromCC3Vector(self.originalValue)];
//}
//
//@end

CC3OpenGLES11StateTrackerVector::CC3OpenGLES11StateTrackerVector()
{
	
}	

CC3OpenGLES11StateTrackerVector::~CC3OpenGLES11StateTrackerVector()
{
	
}

void CC3OpenGLES11StateTrackerVector::setValue(CC3Vector aValue)
{
	bool wasSet = attemptSetValue(aValue);
	logSetValue(wasSet);
}

CC3Vector CC3OpenGLES11StateTrackerVector::getValue()
{
	return value;
}

void CC3OpenGLES11StateTrackerVector::setOriginalValue(CC3Vector aValue)
{
	originalValue = aValue;
}

CC3Vector CC3OpenGLES11StateTrackerVector::getOriginalValue()
{
	return originalValue;
}

bool CC3OpenGLES11StateTrackerVector::attemptSetValue(CC3Vector aVector)
{
	if (!valueIsKnown || !CC3VectorsAreEqual(aVector, value)) 
	{
		value = aVector;
		valueIsKnown = true;
		setGLValue();
		return true;
	} 
	else
	{
		return false;
	}		
}

void CC3OpenGLES11StateTrackerVector::getGLValue()
{
	glGetFloatv(name, (GLfloat*)&originalValue);
}

void CC3OpenGLES11StateTrackerVector::logSetValue(bool wasSet)
{
}

void CC3OpenGLES11StateTrackerVector::logGetGLValue()
{
}

void CC3OpenGLES11StateTrackerVector::restoreOriginalValue()
{
	setValue(originalValue);
}

#pragma mark -
#pragma mark CC3OpenGLES11StateTrackerVector4

//@implementation CC3OpenGLES11StateTrackerVector4
//
//@synthesize value, originalValue;
//
//-(void) setValue: (CC3Vector4) aVector {
//	BOOL wasSet = [self attemptSetValue: aVector];
//	[self logSetValue: wasSet];
//}
//
//-(BOOL) attemptSetValue: (CC3Vector4) aVector {
//	if (!valueIsKnown || !CC3Vector4sAreEqual(aVector, value)) {
//		value = aVector;
//		valueIsKnown = YES;
//		[self setGLValue];
//		return YES;
//	} else {
//		return NO;
//	}	
//}
//
//-(void) logSetValue: (BOOL) wasSet {
//	LogTrace("%@ %@ %@ = %@", [self class], (wasSet ? @"set" : @"reused"),
//			 NSStringFromGLEnum(name), NSStringFromCC3Vector4(value));
//}
//
//-(void) getGLValue {
//	glGetFloatv(name, (GLfloat*)&originalValue);
//}
//
//-(void) logGetGLValue {
//	LogTrace("%@ %@ read GL value %@ (was tracking %@)", 
//			 [self class], NSStringFromGLEnum(name), NSStringFromCC3Vector4(originalValue),
//			 (valueIsKnown ? NSStringFromCC3Vector4(value) : @"UNKNOWN"));
//}
//
//-(void) restoreOriginalValue {
//	self.value = originalValue;
//}
//
//-(NSString*) description {
//	return [NSString stringWithFormat: @"%@ = %@ (orig %@)",
//			[super description], NSStringFromCC3Vector4(self.value), NSStringFromCC3Vector4(self.originalValue)];
//}
//
//@end

CC3OpenGLES11StateTrackerVector4::CC3OpenGLES11StateTrackerVector4()
{
	
}	

CC3OpenGLES11StateTrackerVector4::~CC3OpenGLES11StateTrackerVector4()
{
	
}

void CC3OpenGLES11StateTrackerVector4::setValue(CC3Vector4 aValue)
{
	bool wasSet = attemptSetValue(aValue);
	logSetValue(wasSet);
}

CC3Vector4 CC3OpenGLES11StateTrackerVector4::getValue()
{
	return value;
}

void CC3OpenGLES11StateTrackerVector4::setOriginalValue(CC3Vector4 aValue)
{
	originalValue = aValue;
}

CC3Vector4 CC3OpenGLES11StateTrackerVector4::getOriginalValue()
{
	return originalValue;
}

bool CC3OpenGLES11StateTrackerVector4::attemptSetValue(CC3Vector4 aVector)
{
	if (!valueIsKnown || !CC3Vector4sAreEqual(aVector, value)) 
	{
		value = aVector;
		valueIsKnown = true;
		setGLValue();
		return true;
	} 
	else
	{
		return false;
	}		
}

void CC3OpenGLES11StateTrackerVector4::getGLValue()
{
	glGetFloatv(name, (GLfloat*)&originalValue);
}

void CC3OpenGLES11StateTrackerVector4::logSetValue(bool wasSet)
{
}

void CC3OpenGLES11StateTrackerVector4::logGetGLValue()
{
}

void CC3OpenGLES11StateTrackerVector4::restoreOriginalValue()
{
	setValue(originalValue);
}

#pragma mark -
#pragma mark CC3OpenGLES11StateTrackerComposite

//@implementation CC3OpenGLES11StateTrackerComposite
//
//@synthesize originalValueHandling, shouldAlwaysSetGL;
//
//-(id) init {
//	if ( (self = [super init]) ) {
//		[self initializeTrackers];
//		self.shouldAlwaysSetGL = [[self class] defaultShouldAlwaysSetGL];
//		self.originalValueHandling = [[self class] defaultOriginalValueHandling];
//	}
//	return self;
//}
//
//-(void) initializeTrackers {}
//
//-(void) setOriginalValueHandling: (CC3GLESStateOriginalValueHandling) origValueHandling {
//	originalValueHandling = origValueHandling;
//	self.valueIsKnown = NO;
//}
//
//+(CC3GLESStateOriginalValueHandling) defaultOriginalValueHandling {
//	return kCC3GLESStateOriginalValueIgnore;
//}
//
//-(void) setShouldAlwaysSetGL: (BOOL) aBoolean {
//	shouldAlwaysSetGL = aBoolean;
//	self.valueIsKnown = NO;
//}
//
//+(BOOL) defaultShouldAlwaysSetGL {
//	return NO;
//}
//
//-(BOOL) valueIsKnown {
//	return NO;
//}
//
//-(void) setValueIsKnown:(BOOL) aBoolean {}
//
//-(void) close {
//	if (originalValueHandling == kCC3GLESStateOriginalValueReadOnceAndRestore ||
//		originalValueHandling == kCC3GLESStateOriginalValueReadAlwaysAndRestore) {
//		[self restoreOriginalValue];
//	}
//}
//
//-(void) restoreOriginalValue {}
//
//@end

CC3OpenGLES11StateTrackerComposite::CC3OpenGLES11StateTrackerComposite()
{
	//init();
}

CC3OpenGLES11StateTrackerComposite::~CC3OpenGLES11StateTrackerComposite()
{
}

CC3OpenGLES11StateTrackerComposite* CC3OpenGLES11StateTrackerComposite::init()
{
	initializeTrackers();
	setShouldAlwaysSetGL(defaultShouldAlwaysSetGL());
	setOriginalValueHandling(defaultOriginalValueHandling());
	
	return this;
}

void CC3OpenGLES11StateTrackerComposite::initializeTrackers()
{
}

void CC3OpenGLES11StateTrackerComposite::close()
{
	if (originalValueHandling == kCC3GLESStateOriginalValueReadOnceAndRestore ||
		originalValueHandling == kCC3GLESStateOriginalValueReadAlwaysAndRestore) 
	{
		restoreOriginalValue();
	}
}

void CC3OpenGLES11StateTrackerComposite::setOriginalValueHandling(CC3GLESStateOriginalValueHandling origValueHandling)
{
	originalValueHandling = origValueHandling;
	setValueIsKnown(false);
}

CC3GLESStateOriginalValueHandling CC3OpenGLES11StateTrackerComposite::getOriginalValueHandling()
{
	return originalValueHandling;
}

CC3GLESStateOriginalValueHandling CC3OpenGLES11StateTrackerComposite::defaultOriginalValueHandling()
{
	return kCC3GLESStateOriginalValueIgnore;
}

bool CC3OpenGLES11StateTrackerComposite::defaultShouldAlwaysSetGL()
{
	return false;
}

void CC3OpenGLES11StateTrackerComposite::restoreOriginalValue()
{
}

void CC3OpenGLES11StateTrackerComposite::setValueIsKnown(bool aValueIsKnown)
{
}

bool CC3OpenGLES11StateTrackerComposite::getValueIsKnown()
{
	return false;
}

void CC3OpenGLES11StateTrackerComposite::setShouldAlwaysSetGL(bool aBoolean) 
{
	shouldAlwaysSetGL = aBoolean;
	setValueIsKnown(false);
}

bool CC3OpenGLES11StateTrackerComposite::getShouldAlwaysSetGL()
{
	return shouldAlwaysSetGL;
}

#pragma mark -
#pragma mark CC3OpenGLES11StateTrackerManager

//@implementation CC3OpenGLES11StateTrackerManager
//
//-(void) initializeTrackers {}
//
//-(id) initMinimal {
//	return [super init];
//}
//
//-(id) init {
//	if ( (self = [super init]) ) {
//		[self initializeTrackers];
//	}
//	return self;
//}
//
//@end

CC3OpenGLES11StateTrackerManager::CC3OpenGLES11StateTrackerManager()
{
	init();
}

CC3OpenGLES11StateTrackerManager::~CC3OpenGLES11StateTrackerManager()
{
}

CC3OpenGLES11StateTrackerManager* CC3OpenGLES11StateTrackerManager::init()
{
	initializeTrackers();
	
	return this;
}

CC3OpenGLES11StateTrackerManager* CC3OpenGLES11StateTrackerManager::initMinimal()
{
	return this;
}

void CC3OpenGLES11StateTrackerManager::initializeTrackers()
{
}

CC3OpenGLES11StateTrackerManager* CC3OpenGLES11StateTrackerManager::tracker()
{
	CC3OpenGLES11StateTrackerManager* pState = new CC3OpenGLES11StateTrackerManager;
	if (pState) 
	{
		pState->autorelease();
	}
	
	return pState;
}


